package com.jy.wms.controller;

import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.ExportUtils;
import com.jy.wms.common.base.BaseController;
import com.jy.wms.common.base.BaseSessionInfo;
import com.jy.wms.common.base.JsonResult;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsOrderDistribution;
import com.jy.wms.pojo.WmsOrderDistributionStatistics;
import com.jy.wms.service.StatisticsOfDistributionOfProvincesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/StatisticsOfDistributionOfProvinces")
@Api(value = "/StatisticsOfDistributionOfProvinces", description = "省市订单分布统计报表")
public class StatisticsOfDistributionOfProvincesController extends BaseController<StatisticsOfDistributionOfProvincesService, WmsOrderDistribution> {

    @PostMapping(value = "OrderDistriButionStatistics")
    @ApiOperation(value = "各省订单分布统计（根据时间查询）", response = JSONObject.class, notes = "各省订单分布统计")
    public @ResponseBody
    JSONObject orderdistributionstatistics(@RequestBody WmsOrderDistribution WmsOrderDistribution, HttpSession session) {
        JSONObject json = null;

        //获取当前操作用户的货主id
        List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = hz.get(0).getHzid();
        //获取仓库Id
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid = userinfo.getUser().getOrgId().get(0).toString();
        try {
            json = baseService.orderdistributionstatistics(WmsOrderDistribution, hzid, ckid);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json;
    }

    @PostMapping(value = "OrderDistriButionStatisticsExcel")
    @ApiOperation(value = "各省订单分布统计报表导出", response = JSONObject.class, notes = "各省订单分布统计报表导出")
    public @ResponseBody
    JsonResult baseSelectBathPrimaryKeys(@RequestBody WmsOrderDistributionStatistics WmsOrderDistributionStatistics, HttpServletResponse response, HttpServletRequest request, HttpSession session) throws IOException {
        String filename = "各省订单分布统计";
        //获取当前操作用户的货主id
        List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = hz.get(0).getHzid();
        //获取仓库Id
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid = userinfo.getUser().getOrgId().get(0).toString();

        WmsOrderDistributionStatistics.setHzid(hzid);
        WmsOrderDistributionStatistics.setCkid(ckid);
        List<com.jy.wms.pojo.WmsOrderDistributionStatistics> order = null;
        try {
            //后台接收数据
            order = baseService.baseSelectBathPrimaryKeys(WmsOrderDistributionStatistics, hzid, ckid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ExportUtils reportExcel = new ExportUtils();
        reportExcel.excelExport(order, filename, com.jy.wms.pojo.WmsOrderDistributionStatistics.class, 1, response, request);

        return renderSuccess("报表成功导出");
    }

}
